﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace Base.ModuleBase.Converters
{
    [ValueConversion(typeof(bool), typeof(bool))]
    public class BoolInvertConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value is false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value is false;
        }
    }

    [ValueConversion(typeof(bool), typeof(Visibility))]
    public class BoolToVisibilityConverter : ConverterBase
    {
        /// <summary>
        /// 是否反转转换结果
        /// </summary>
        public bool Invert { get; set; }

        public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var tempBool = value is true;

            return Invert ?
                tempBool ? Visibility.Collapsed : Visibility.Visible :
                tempBool ? Visibility.Visible : Visibility.Collapsed;
        }

        public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value is Visibility.Visible ? !Invert : Invert;
        }
    }

    /// <summary>
    /// 转换可见性
    /// </summary>
    public class ValueParmToVisbleConverter : ConverterBase
    {
        /// <summary>
        /// 是否反转转换结果
        /// </summary>
        public bool Invert { get; set; }

        public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Invert ?
                value.Equals(parameter) ? Visibility.Collapsed : Visibility.Visible :
                value.Equals(parameter) ? Visibility.Visible : Visibility.Collapsed;
        }

        public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value is Visibility.Visible ? parameter : Binding.DoNothing;
        }
    }

    /// <summary>
    /// 转换Bool
    /// </summary>
    [ValueConversion(typeof(object), typeof(bool))]
    public class ValueParmToBoolConverter : ConverterBase
    {
        /// <summary>
        /// 是否反转转换结果
        /// </summary>
        public bool Invert { get; set; }

        public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Invert ? !value.Equals(parameter) : value.Equals(parameter);
        }

        public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value is true ? parameter : Binding.DoNothing;
        }
    }

    /// <summary>
    /// 是否大于0
    /// </summary>
    public class IsBigZeroConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value is int v && v > 0;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class NumberToMinusConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value is double v ? -v : 0;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
